﻿package mortal.game.view.mail.view
{
    import Message.Game.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.mvc.core.*;

    public class MailReadPanel extends GSprite
    {
        private var _bg:ScaleBitmap;
        private var _bg2:ScaleBitmap;
        private var _txtName1:GTextFiled;
        private var _txtName2:TextBox;
        private var _txtTitle1:GTextFiled;
        private var _txtTitle2:TextBox;
        private var _txtContent:GTextArea;
        private var _warnBg:ScaleBitmap;
        private var _bmpWarn:GBitmap;
        private var _bmpWarn2:GBitmap;
        private var _bmpYuanbao:GBitmap;
        private var _bmpTongqian:GBitmap;
        private var _goldTextBox:TextBox;
        private var _coinTextBox:TextBox;
        private var _txtItemStatic:GTextFiled;
        private var _itemList:GTileList;
        private var _line:ScaleBitmap;
        private var _btnGetAttachment:GButton;
        public var btnReply:GButton;
        private var _data:SMail;

        public function MailReadPanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.bg(0, 0, 311, 290, this);
            this._txtName1 = UIFactory.textField("发件人：", 6, 5, 54, 20, this, null, false, false);
            this._txtName1.textColor = 16777215;
            this._txtTitle1 = UIFactory.textField("标   题：", 6, 28, 54, 20, this, null, false, false);
            this._txtTitle1.textColor = 16777215;
            this._txtName2 = this.createTextBox(0, 0, 242, 20);
            this._txtName2.x = this._txtName1.x + this._txtName1.width;
            this._txtName2.y = this._txtName1.y;
            this._txtName2._text.selectable = true;
            this._txtTitle2 = this.createTextBox(0, 0, 242, 20);
            this._txtTitle2.x = this._txtTitle1.x + this._txtTitle1.width;
            this._txtTitle2.y = this._txtName2.y + 24;
            this._txtTitle2._text.selectable = true;
            this._bg2 = UIFactory.bg(this._txtName1.x + 2, this._txtTitle1.y + this._txtTitle2.height + 5, 295, 155, this, "InputDisablBg");
            this._warnBg = UIFactory.bg(this._bg2.x, this._bg2.y + 2, 300, 19, this, "TextBg");
            this._txtContent = UIFactory.textArea("", this._txtName1.x + 3, 74, this._bg2.width - 2, 125, this);
            this._txtItemStatic = UIFactory.textField("", 10, 212, 17, 40, this, null, false, false);
            this._txtItemStatic.wordWrap = true;
            this._txtItemStatic.multiline = true;
            this._txtItemStatic.textColor = 16777215;
            this._txtItemStatic.htmlText = "物品";
            this._itemList = UIFactory.tileList(30, 212, 270, 70, this);
            this._itemList.rowHeight = 57;
            this._itemList.columnWidth = 45;
            this._itemList.horizontalGap = 0;
            this._itemList.verticalGap = -3;
            this._itemList.columnCount = 6;
            this._itemList.rowCount = 1;
            this._itemList.setStyle("cellRenderer", MailAttachRenderer);
            this._itemList.horizontalScrollPolicy = ScrollPolicy.ON;
            this._itemList.direction = ScrollBarDirection.HORIZONTAL;
            this.addChild(this._itemList);
            this._itemList.selectedIndex = 1;
            this._goldTextBox = this.createTextBox(0, 0, 92, 20);
            this._goldTextBox.x = 30;
            this._goldTextBox.y = this._itemList.y + 54;
            this._goldTextBox._text.textColor = GlobalStyle.colorHuangUint;
            this._bmpYuanbao = UIFactory.bitmap(ImagesConst.Yuanbao, this._goldTextBox.x - 20, (this._goldTextBox.y + 1), this);
            this._coinTextBox = this.createTextBox(0, 0, 92, 20);
            this._coinTextBox.x = 145;
            this._coinTextBox.y = this._itemList.y + 54;
            this._goldTextBox._text.textColor = GlobalStyle.colorHuangUint;
            this._bmpTongqian = UIFactory.bitmap(ImagesConst.Jinbi, this._coinTextBox.x - 21, this._coinTextBox.y + 2, this);
            this._line = UIFactory.bg(0, 257, 295, 1, this, "SplitLine");
            this._btnGetAttachment = UIFactory.gButton("提取附件", 238, 265, 63, 22, this);
            this.btnReply = UIFactory.gButton("回复", 251, 263, 50, 22, this);
            this._btnGetAttachment.configEventListener(MouseEvent.CLICK, this._btnGetAttachmentClickHandler);
            this._itemList.doubleClickEnabled = true;
            this._itemList.configEventListener(MouseEvent.DOUBLE_CLICK, this.itemListDoubleClick);
            this.showMail(null);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._data = null;
            this._bg.dispose(param1);
            this._txtName1.dispose(param1);
            this._txtTitle1.dispose(param1);
            this._txtName2.dispose(param1);
            this._txtTitle2.dispose(param1);
            this._bg2.dispose(param1);
            this._warnBg.dispose(param1);
            if (this._bmpWarn)
            {
                this._bmpWarn.dispose(param1);
            }
            if (this._bmpWarn2)
            {
                this._bmpWarn2.dispose(param1);
            }
            this._txtContent.dispose(param1);
            this._txtItemStatic.dispose(param1);
            this._itemList.dispose(param1);
            this._goldTextBox.dispose(param1);
            this._bmpYuanbao.dispose(param1);
            this._coinTextBox.dispose(param1);
            this._bmpTongqian.dispose(param1);
            this._line.dispose(param1);
            this._btnGetAttachment.dispose(param1);
            this.btnReply.dispose(param1);
            this._bg = null;
            this._txtName1 = null;
            this._txtTitle1 = null;
            this._txtName2 = null;
            this._txtTitle2 = null;
            this._bg2 = null;
            this._warnBg = null;
            this._bmpWarn = null;
            this._bmpWarn2 = null;
            this._txtContent = null;
            this._txtItemStatic = null;
            this._itemList = null;
            this._goldTextBox = null;
            this._bmpYuanbao = null;
            this._coinTextBox = null;
            this._bmpTongqian = null;
            this._line = null;
            this._btnGetAttachment = null;
            this.btnReply = null;
            return;
        }// end function

        public function showMail(param1:SMail) : void
        {
            var _loc_2:String = null;
            this._data = param1;
            if (param1 == null)
            {
                this._txtName2._text.text = "";
                this._txtTitle2._text.text = "";
                this._txtContent.text = "";
                this._btnGetAttachment.visible = false;
                this.btnReply.visible = false;
                this._bmpTongqian.visible = true;
                this._bmpYuanbao.visible = true;
                this._coinTextBox.visible = true;
                this._coinTextBox._text.text = "";
                this._goldTextBox.visible = true;
                this._goldTextBox._text.text = "";
                this._btnGetAttachment.visible = Cache.instance.mail.checkMailhadAttach(param1);
                this._itemList.dataProvider = this.getDataProvider();
                this._itemList.visible = true;
                this._bg2.height = 155 + 49;
                this._txtContent.height = 125 + 49;
                if (this._bmpWarn)
                {
                    this._bmpWarn.dispose();
                    this._bmpWarn = null;
                }
                if (this._bmpWarn2)
                {
                    this._bmpWarn2.dispose();
                    this._bmpWarn2 = null;
                }
                return;
            }
            else if (param1.status == 1)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MailRead, param1.mailId));
            }
            var _loc_3:Boolean = false;
            if (this._data.type == EMailType._EMailTypeSystem)
            {
                this._bg2.height = 155;
                this._txtContent.height = 125;
                this._txtItemStatic.visible = true;
                this._itemList.visible = true;
                this._coinTextBox.visible = true;
                this._goldTextBox.visible = true;
                this._bmpTongqian.visible = true;
                this._bmpYuanbao.visible = true;
                this._txtName2._text.text = SysMsgConfig.instance.getInfoByKey("sender_system").msgValue;
                this.btnReply.visible = false;
                this._txtTitle2._text.text = this._data.title;
                this._txtContent.htmlText = this._data.content;
                _loc_2 = ImagesConst.mailSysMail;
            }
            else
            {
                _loc_3 = true;
                this._bg2.height = 155 + 49;
                this._txtContent.height = 125 + 49;
                this._txtItemStatic.visible = false;
                this._itemList.visible = false;
                this._coinTextBox.visible = false;
                this._goldTextBox.visible = false;
                this._bmpTongqian.visible = false;
                this._bmpYuanbao.visible = false;
                if (this._data.type == EMailType._EMailTypePlayer)
                {
                    this._txtTitle2._text.text = FilterText.instance.getFilterStr(this._data.title);
                }
                else if (this._data.type == EMailType._EMailTypeGuild)
                {
                    this._txtTitle2._text.text = "[公会邮件]" + FilterText.instance.getFilterStr(this._data.title);
                }
                this._txtName2._text.text = this._data.fromPlayerName;
                this.btnReply.visible = true;
                this._txtContent.text = FilterText.instance.getFilterStr(this._data.content);
                if (Cache.instance.friend.findRecordByRoleId(param1.fromPlayerId) == null)
                {
                    _loc_2 = ImagesConst.mailWarn;
                }
                else
                {
                    _loc_2 = ImagesConst.mailPersMail;
                }
            }
            if (this._bmpWarn)
            {
                this._bmpWarn.dispose();
                this._bmpWarn = null;
            }
            if (this._bmpWarn2)
            {
                this._bmpWarn2.dispose();
                this._bmpWarn2 = null;
            }
            this._bmpWarn = UIFactory.bitmap(_loc_2, 0, 55, this);
            this._bmpWarn.x = (this._bg2.width - this._bmpWarn.width) / 2;
            if (_loc_3)
            {
                this._bmpWarn2 = UIFactory.bitmap(ImagesConst.mailWarn2, 0, 76, this);
                this._bmpWarn2.x = (this._bg2.width - 289) / 2 + 15;
                this._txtContent.y = 74 + 42;
            }
            else
            {
                this._txtContent.y = 74;
            }
            this._itemList.dataProvider = this.getDataProvider();
            this._coinTextBox._text.text = this._data.attachmentCoin.toString();
            this._goldTextBox._text.text = this._data.attachmentGold.toString();
            this._btnGetAttachment.visible = Cache.instance.mail.checkMailhadAttach(param1);
            return;
        }// end function

        public function checkMail() : void
        {
            if (this._data == null)
            {
                this.showMail(null);
                return;
            }
            if (Cache.instance.mail.getMailByMailId(this._data.mailId) == null)
            {
                this.showMail(null);
            }
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:* = undefined;
            var _loc_1:* = new DataProvider();
            var _loc_2:int = 40;
            var _loc_3:int = 6;
            if (this._data && this._data.playerItems && this._data.playerItems.length > 0)
            {
                _loc_4 = this._data.playerItems.length;
                _loc_4 = _loc_4 < _loc_2 ? (_loc_4) : (_loc_2);
                _loc_4 = _loc_4 < _loc_3 ? (_loc_3) : (_loc_4);
                _loc_5 = 0;
                while (_loc_5 < _loc_4)
                {
                    
                    if (this._data.playerItems[_loc_5] != null)
                    {
                        _loc_6 = new ItemData(this._data.playerItems[_loc_5]);
                    }
                    else
                    {
                        _loc_6 = new Object();
                    }
                    _loc_1.addItem(_loc_6);
                    _loc_5++;
                }
            }
            else
            {
                _loc_4 = _loc_3;
                _loc_5 = 0;
                while (_loc_5 < _loc_4)
                {
                    
                    _loc_1.addItem(new Object());
                    _loc_5++;
                }
            }
            return _loc_1;
        }// end function

        private function createTextBox(param1:int, param2:int, param3:int = 117, param4:int = 23) : TextBox
        {
            var _loc_5:* = UICompomentPool.getUICompoment(TextBox);
            _loc_5.createDisposedChildren();
            _loc_5.textFieldHeight = 18;
            _loc_5.textFieldWidth = 80;
            _loc_5.bgHeight = param4;
            _loc_5.bgWidth = param3;
            _loc_5.x = param2 * 120;
            _loc_5.y = 38 + param1 * 26;
            var _loc_6:* = new GTextFormat();
            _loc_6.color = 16777215;
            _loc_5.setbgStlye("InputDisablBg", _loc_6);
            this.addChild(_loc_5);
            return _loc_5;
        }// end function

        private function _btnGetAttachmentClickHandler(event:MouseEvent) : void
        {
            if (Cache.instance.pack.backPackCache.isPackFull())
            {
                return MsgManager.showRollTipsMsg("背包已满", MsgRollTipsType.msgRollTips3);
            }
            if (Cache.instance.pack.backPackCache.leftCapacity < Cache.instance.mail.getMailAttachItemCount(this.data))
            {
                MsgManager.showRollTipsMsg("背包空位不足");
                return;
            }
            var _loc_2:Object = {};
            _loc_2.mailId = this._data.mailId;
            _loc_2.attachementIndex = 0;
            _loc_2.isDelete = Cache.instance.mail.mailGetAfterDelete;
            Dispatcher.dispatchEvent(new DataEvent(EventName.MailGetAttachment, _loc_2));
            return;
        }// end function

        private function itemListDoubleClick(event:MouseEvent) : void
        {
            if (this._data == null)
            {
                return;
            }
            if (this._data.playerItems.length <= this._itemList.selectedIndex)
            {
                return;
            }
            if (Cache.instance.pack.backPackCache.isPackFull())
            {
                MsgManager.showRollTipsMsg("背包已满");
                return;
            }
            var _loc_2:Object = {};
            _loc_2.mailId = this._data.mailId;
            _loc_2.attachementIndex = this._itemList.selectedIndex + 1;
            _loc_2.isDelete = Cache.instance.mail.mailGetAfterDelete;
            Dispatcher.dispatchEvent(new DataEvent(EventName.MailGetAttachment, _loc_2));
            return;
        }// end function

        public function get data() : SMail
        {
            return this._data;
        }// end function

    }
}
